var t = require("../@babel/runtime/helpers/interopRequireDefault.js")(require("../@babel/runtime/helpers/typeof.js")),
	e = require("./jquery.js");

module.exports = {
	toQueryPair: function(t, e) {
		return void 0 === e ? t : t + "=" + encodeURIComponent(null === e ? "" : String(e));
	},
	getUrl: function(n, o, i) {
		n = n.replace(/\//gi, ".");
		var a = getApp().globalData.getConfig().api + "&r=" + n;
		return o && ("object" == (0, t.default)(o) ? a += "&" + e.param(o) : "string" == typeof o && (a += "&" + o)), a;
	},
	json: function(t, n, o, i, a, r) {
		var s = getApp().globalData,
			c = s.getCache("userinfo_openid"),
			u = s.getCache("usermid"),
			token= s.getCache("user_token"),
			f = s.getCache("authkey");
			
			// #ifdef MP-WEIXIN
			(n = n || {}).comefrom = "wxapp", n.openid = "sns_wa_" + c, u && (n.mid = u.mid, n.merchid = n.merchid || u.merchid);
			// #endif
			// #ifdef H5
			(n = n || {}).comefrom = "jxh5", n.openid = "sns_wa_" + c, u && (n.mid = u.mid, n.merchid = n.merchid || u.merchid);
			// #endif
			// #ifdef APP-PLUS

			(n = n || {}).comefrom = "jxapp", n.openid = "sns_wa_" + c, u && (n.mid = u.mid, n.merchid = n.merchid || u.merchid);
			//版本处理
			s.getCache("version")?n.version = s.getCache("version"):plus.runtime.getProperty(plus.runtime.appid, function(wgtInfo) {s.setCache("version",wgtInfo.versionCode),n.version = wgtInfo.versionCode});
			// #endif
			// #ifdef MP-TOUTIAO
			(n = n || {}).comefrom = "ttapp", n.openid = "sns_wa_" + c, u && (n.mid = u.mid, n.merchid = n.merchid || u.merchid);
			// #endif
			n.openid='ol0up1I7N6XsCMbN3YKdjtWwBewk'
			n.token=token
			n.mtoken=203
			
		var d = this;
		i && d.loading(), n && (n.authkey = f || "");
		var l = {
			url: (a ? this.getUrl(t) : this.getUrl(t, n)) + "&timestamp=" + +new Date(),
			method: a ? "POST" : "GET",
			header: {
				"Content-type": a ? "application/x-www-form-urlencoded" : "application/x-www-form-urlencoded",
				//	'Cookie': "PHPSESSID=" + c 
				//application/json
			}
		};
		// #ifdef APP-PLUS
		console.log(l.url);
		// #endif
		r || delete l.header.Cookie, a && (l.data = e.param(n)), o && (l.success = function(t) {
			if (i && d.hideLoading(), "request:ok" == t.errMsg && "function" == typeof o) {
				if (s.setCache("authkey", t.data.authkey || ""), void 0 !== t.data.sysset) {
					if (1 == t.data.sysset.isclose) return void uni.redirectTo({
						url: "/pages/message/auth/index?close=1&text=" + t.data.sysset.closetext
					});
					s.setCache("sysset", t.data.sysset);
				}

				o(t.data);
			}
		}), l.fail = function(t) {
			i && d.hideLoading(), d.alert(t.errMsg);
		};
		
		
		uni.request(l);
	},
	post: function(t, e, n, o, i) {
		this.json(t, e, n, o, !0, i);
	},
	get: function(t, e, n, o, i) {
		this.json(t, e, n, o, !1, i);
	},
	getDistanceByLnglat: function(t, e, n, o) {
		function i(t) {
			return t * Math.PI / 180;
		}

		var a = i(e),
			r = i(o),
			s = a - r,
			c = i(t) - i(n),
			u = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(s / 2), 2) + Math.cos(a) * Math.cos(r) * Math.pow(Math.sin(c / 2), 2)));
		return u *= 6378137, u = Math.round(1e4 * u) / 1e7;
	},
	alert: function(e, n) {
		if (typeof(n) == "undefined") {
			n = function() {

			}
		}
		"object" === (0, t.default)(e) && (e = JSON.stringify(e)), uni.showModal({
			title: "提示",
			content: e,
			showCancel: !1,
			success: function(t) {
				t.confirm && "function" == typeof confirm && n();
			}
		});
	},
	confirm: function(e, n, o) {
		"object" === (0, t.default)(e) && (e = JSON.stringify(e)), uni.showModal({
			title: "提示",
			content: e,
			showCancel: !0,
			success: function(t) {
				t.confirm ? "function" == typeof n && n() : "function" == typeof o && o();
			}
		});
	},
	loading: function(t) {
		void 0 !== t && "" != t || (t = "加载中"), uni.showToast({
			title: t,
			icon: "loading",
			duration: 5e6
		});
	},
	hideLoading: function() {
		uni.hideToast();
	},
	toast: function(t, e) {

		e || (e = "success"), uni.showToast({
			title: t,
			icon: e,
			duration: 1e3
		});
	},
	success: function(t) {
		uni.showToast({
			title: t,
			icon: "success",
			duration: 1e3
		});
	},
	upload: function(t,a) {
		var e = this;
		console.log(a);
		uni.chooseImage({
			count:a.count,
			sizeType: a.sizeType,
			sourceType:a.sourceType, 
			success: function(n) {
				e.loading("正在上传...");
				var o = e.getUrl("util/uploader/upload", {
						file: "file",
						type:a.type||1,
						idcard:a.iscard||0,
					}),
					i = n.tempFilePaths;
					console.log(o);
				uni.uploadFile({
					url: o,
					filePath: i[0],
					name: "file",
					success: function(n) {
						e.hideLoading();
						var o = JSON.parse(n.data);
						if (0 != o.error) e.alert("上传失败");
						else if ("function" == typeof t) {
							var i = o.files[0];
							t(i);
						}
					}
				});
			}
		});
	},
	pdata: function(t) {
		return t.currentTarget.dataset;
	},
	data: function(t) {
		return t.target.dataset;
	},
	phone: function(t) {
		var e = this.pdata(t).phone;
		uni.makePhoneCall({
			phoneNumber: e
		});
	},
	pay: function(e, n, o) {
		return "object" == (0, t.default)(e) && "function" == typeof n && (e.success = n, "function" == typeof o && (e.fail =
			o), console.log(e),void uni.requestPayment(e));
	},
	cartcount: function(t) {
		this.get("member/cart/count", {}, function(e) {
			t.setData({
				cartcount: e.cartcount
			});
		});
	},
	onShareAppMessage: function(t, e) {
		var n = getApp().globalData,
			o = n.getCache("sysset"),
			i = o.share || {},
			a = n.getCache("userinfo_id"),
			r = o.shopname || "",
			s = o.description || "";
			console.log((t = -1 != (t = t || "/pages/index/index").indexOf("?") ? t + "&" : t + "?") + "mid=" + a);
		return i.title && (r = i.title), e && (r = e), i.desc && (s = i.desc), {
			title: r,
			desc: s,
			path: (t = -1 != (t = t || "/pages/index/index").indexOf("?") ? t + "&" : t + "?") + "mid=" + a
		};
	},
	onShareTimeline: function(t, e,f) {
		var n = getApp().globalData,
			o = n.getCache("sysset"),
			i = o.share || {},
			a = n.getCache("userinfo_id"),
			r = o.shopname || "",
			s = o.description || "";
			console.log('id='+(t = -1 != (t = t || "/pages/index/index").indexOf("?") ? t + "&" : t) + "mid=" + a);
		return i.title && (r = i.title), e && (r = e), i.desc && (s = i.desc), {
			title: r,
			imageUrl:f,
			query: 'id='+(t = -1 != (t = t || "/pages/index/index").indexOf("?") ? t + "&" : t) + "mid=" + a
		};
	},
	
	str2Obj: function(t) {
		if ("string" != typeof t) return t;
		if (t.indexOf("&") < 0 && t.indexOf("=") < 0) return {};
		var n = t.split("&"),
			o = {};
		return e.each(n, function(t, e) {
			if (e.indexOf("=") > -1) {
				var n = e.split("=");
				o[n[0]] = n[1];
			}
		}), o;
	},
	countDown: function(t, e) {
		var n = parseInt(Date.now() / 1e3),
			o = 0;
		if (t && (o = t > n ? t - n : n - t, o = parseInt(o)), e && (o = parseInt(e)), 0 == o) return !1;
		var i = Math.floor(o / 86400),
			a = Math.floor((o - 24 * i * 60 * 60) / 3600),
			r = Math.floor((o - 24 * i * 60 * 60 - 3600 * a) / 60),
			s = Math.floor(o - 24 * i * 60 * 60 - 3600 * a - 60 * r);
		return [i, a < 10 ? "0" + a : a, r < 10 ? "0" + r : r, s < 10 ? "0" + s : s];
	}
};
